import Vue from 'vue'
import Router from 'vue-router'
import LoginView from '../components/LoginView'
import Home from '../components/Home'
import Welcome from "../components/Welcome";
import TodayWork from "../components/TodayWork";
import Task from "../components/Task";
import DataAnalyze from "../components/DataAnalyze";
import Watch from "../components/Watch";
import WXJH from "../components/WXJH";
import SGJG from "../components/SGJG";
import CreateSCRW from "../components/CreateSCRW";
import SelectSCRW from "../components/SelectSCRW";
import Server from "../components/Server";
import User from "../components/server/User";
import FangHuYuan from "../components/server/FangHuYuan";
import FuZeRen from "../components/server/FuZeRen";
import LianLuoYuan from "../components/server/LianLuoYuan";
import CheJian from "../components/server/CheJian";
import GongQu from "../components/server/GongQu";

Vue.use(Router)

const router = new Router({
  routes: [
    {path: '/', redirect: '/login'},
    {path: '/login', component: LoginView},
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {path: '/welcome',component:Welcome},
        {
          path : '/task',
          component : Task,
          redirect : '/createSCRW',
          children:[
            {path: '/createSCRW',component: CreateSCRW},
            {path: '/selectSCRW',component: SelectSCRW},
          ]
        },
        {
          path : '/todayWork',
          component : TodayWork,
          redirect : '/sgjg',
          children : [
            { path:'/sgjg',component: SGJG},
            { path:'/wxjh',component: WXJH},
          ]

        },
        {path: '/dataAnalyze', component: DataAnalyze},
        {path: '/watch', component: Watch},
        {
          path: '/server',
          component: Server,
          redirect: '/user',
          children: [
            {path: '/user',component: User},
            {path: '/fangHuYuan',component: FangHuYuan},
            {path: '/fuZeRen',component: FuZeRen},
            {path: '/lianLuoYuan',component: LianLuoYuan},
            {path: '/cheJian',component: CheJian},
            {path: '/gongQu',component: GongQu},
          ]
        }
      ],
    }
  ]
})

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
